/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io;

import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.HeaderEntity;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.StringParameter;
import org.openaecfm.spf.model.FileDescription;

/**
 * Input and output FileDescription.
 * @author Qiu Kuining
 */
public final class FileDescriptionIO {

    /**
     * Export FileDescription.
     * @param pojo
     * @return
     */
    public HeaderEntity exportSPF(FileDescription pojo) {
        HeaderEntity he = new HeaderEntity("FILE_DESCRIPTION");

        // Set Description
        ListParameter description = new ListParameter();
        for (String s : pojo.getDescription()) {
            description.add(new StringParameter(s, StringParameter.UTF_8));
        }
        he.addParameter(description);

        // Set Implementation_level
        he.addParameter(new StringParameter(pojo.getImplementation_level(), StringParameter.UTF_8));

        return he;
    }

    /**
     * Import FileDescription.
     * @param he
     * @return
     */
    public FileDescription importSPF(HeaderEntity he) {
        FileDescription pojo = new FileDescription();

        // Set Description
        ListParameter description = (ListParameter) he.getParameter(0);
        for (Parameter p : description) {
            StringParameter s = (StringParameter) p;
            pojo.addDescription(s.toUTF8());
        }

        // Set Implementation_level
        StringParameter impl_level = (StringParameter) he.getParameter(1);
        pojo.setImplementation_level(impl_level.toUTF8());

        return pojo;
    }
}
